package com.pricetag.components;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.ImageView;

import com.pricetag.exceptions.SearchRecordNotExistsException;
import com.pricetag.pricetagandroid.SearchHistoryListActivity;
import com.pricetag.search.SearchHandler;

@SuppressLint("ValidFragment")
public class ShowImageDialogFragment extends DialogFragment {
	private String searchRecordID;

	@SuppressLint("ValidFragment")
	public ShowImageDialogFragment(String searchRecordID) {
		super();
		this.searchRecordID = searchRecordID;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		ImageView imageView = new ImageView(getActivity());
		Uri uri = Uri.fromFile(new File(getActivity().getFilesDir(), SearchHandler.getSearchRecord(searchRecordID)
				.getImageFilename()));
		imageView.setImageURI(uri);
		builder.setView(imageView).setTitle(SearchHandler.getSearchRecord(searchRecordID).getDateCreated().toString())
				.setNegativeButton("OK", null);
		if (getActivity() instanceof SearchHistoryListActivity) {
			builder.setPositiveButton("Delete", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					SearchHandler.getSearchRecord(searchRecordID).setDeleted(true);
					((SearchHistoryListActivity) getActivity()).onDelete();
				}
			});
			builder.setNeutralButton("Search Again", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						new SearchHandler(searchRecordID, getActivity()).startImageSearchAgain();
					} catch (SearchRecordNotExistsException e) {
						e.printStackTrace();
					}
				}

			});
		}

		return builder.create();
	}
}
